#pragma once

#include "create_mysql_client.hpp"
//#include "message.hxx"
#include "message-odb.hxx"
#include "logger.hpp"

namespace xu
{
    class ManageMessage
    {
    public:
        using ptr = std::shared_ptr<ManageMessage>;
        ManageMessage(std::shared_ptr<odb::mysql::database> db)
        :_db(db)
        {}

        bool Insert(Message& msg)
        {
            try 
            {
                //获取事务对象开启事务
                odb::transaction trans(_db->begin());
                _db->persist(msg);
                //提交事务
                trans.commit();
            }
            catch (std::exception &e) 
            {
                LOG_ERROR("插入消息数据出错： {}", e.what() );
                return false;
            }
            LOG_INFO("插入消息数据成功");
            return true;
        }

        void Delete(const std::string &ssid)
        {
            try 
            {
                // 开始事务
                odb::transaction t(_db->begin());
                typedef odb::query<Message> query;
                _db->erase_query<Message>(query::to_session_id == ssid);
                // 提交事务
                t.commit();

            } 
            catch (const odb::exception& e) 
            {
                LOG_ERROR("删除用户数据出错： {}", e.what() );
                return ;
            }

            LOG_INFO("删除用户数据成功");
        }

        std::vector<Message> SelectMessageN(const std::string &ssid,int n)
        {
            std::vector<Message> res;
            try 
            {
                odb::transaction trans(_db->begin());
                typedef odb::query<Message> query;
                typedef odb::result<Message> result;
                std::string sql = "to_session_id='" +  ssid + "' order by created_time desc limit " + std::to_string(n);  
                //LOG_DEBUG("{}",sql);
                result r(_db->query<Message>(sql));
                for (result::iterator i(r.begin()); i != r.end(); ++i) {
                    res.push_back(*i);
                }
                trans.commit();
            }
            catch (std::exception &e) 
            {
                LOG_ERROR("查找前:{} 失败:{}！",n, e.what());
                return std::vector<Message>();
            }
           
            LOG_INFO("查找前:{} 成功！",n);
            return res;
        }

        std::vector<Message> SelectMessageRange(const std::string &ssid,const boost::posix_time::ptime & stime
        ,const boost::posix_time::ptime & etime)
        {
            std::vector<Message> res;
            try 
            {
                odb::transaction trans(_db->begin());
                typedef odb::query<Message> query;
                typedef odb::result<Message> result;
                result r(_db->query<Message>(query::to_session_id == ssid && query::created_time >= stime &&
                query::created_time <= etime));
                for (result::iterator i(r.begin()); i != r.end(); ++i) {
                    res.push_back(*i);
                }
                trans.commit();
            }
            catch (std::exception &e) 
            {
                LOG_ERROR("查找范围失败:{}！", e.what());
                return std::vector<Message>();
            }
            LOG_INFO("查找范围成功！");
            return res;
        }
        
    private:
        std::shared_ptr<odb::mysql::database> _db;
    };
}